<template>
    <h2>添加公告页面</h2>
    <table border="1">
        <tr>
            <td>公告标题</td>
            <td>
                <input type="text" placeholder="请输入公告标题"v-model="info.tName">
            </td>
        </tr>
        <tr>
            <td>公告日期</td>
            <td>
                <input type="date" v-model="info.tDate">
            </td>
        </tr>
        <tr>
            <td>公告内容</td>
            <td>
                <textarea placeholder="请输入公告内容" style="width: 100px;height: 70px;" v-model="info.tinfo">

                </textarea>
            </td>
        </tr>
        <tr>
            <td>公告类型</td>
            <td>
                <select v-model="info.lid">
                    <option :value="-1">请选择</option>
                    <option v-for="item in Lei" :value="item.lid">{{item.lname}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加公告" @click="Add">
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import axios from 'axios';
import { ref ,onMounted} from 'vue';
import { useRouter } from 'vue-router';
const rour=useRouter();
onMounted(()=>{
    Bind();
})
//绑定下拉框
const Lei=ref([{
    "lid": 0,
    "lname": ""
}])
const Bind=()=>{
    axios.get("https://localhost:7218/api/Title/ShowTitleLei")
    .then(res=>{
        Lei.value=res.data
    })
}
const info=ref({
"tid": 0,
  "tName": "",
  "tDate": "",
  "tinfo": "",
  "lid": -1,
})
const Add=()=>{
    if(info.value.tName==""){
        alert("公告标题不能为空");
        return;
    }
    var res=/^[\u4e00-\u9fa5]{1,20}$/
    if(!res.test(info.value.tName)){
        alert("公告标题必须为中文且二十字以内");
        return;
    }
 
    if(info.value.tinfo.length>50){
        alert("公告内容必须50字以内");
        return;
    }
axios.post("https://localhost:7218/api/Title/AddTitle",info.value)
.then(res=>{
    if(res.data>0){
        alert("添加成功");
        rour.push("ShowTitle")
    }
    else{
        alert("添加失败");
    }
})
}
</script>